<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="工人编号" prop="employeeNo">
        <el-input
          v-model="queryParams.employeeNo"
          placeholder="请输入工人编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="xm">
        <el-input
          v-model="queryParams.xm"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="员工年龄" prop="age">
        <el-input
          v-model="queryParams.age"
          placeholder="请输入员工年龄"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="家庭住址" prop="jtzz">
        <el-input
          v-model="queryParams.jtzz"
          placeholder="请输入家庭住址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出生年月" prop="csny">
        <el-date-picker clearable
          v-model="queryParams.csny"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择出生年月">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="电话" prop="tel">
        <el-input
          v-model="queryParams.tel"
          placeholder="请输入电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性别" prop="xb">
        <el-input
          v-model="queryParams.xb"
          placeholder="请输入性别"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号码" prop="sfzhm">
        <el-input
          v-model="queryParams.sfzhm"
          placeholder="请输入身份证号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证正面" prop="sfzdzZm">
        <el-input
          v-model="queryParams.sfzdzZm"
          placeholder="请输入身份证正面"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证反面" prop="sfzdzFm">
        <el-input
          v-model="queryParams.sfzdzFm"
          placeholder="请输入身份证反面"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="头像" prop="tx">
        <el-input
          v-model="queryParams.tx"
          placeholder="请输入头像"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工人类型ID" prop="grlx">
        <el-input
          v-model="queryParams.grlx"
          placeholder="请输入工人类型ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工人类型名称" prop="grlxmc">
        <el-input
          v-model="queryParams.grlxmc"
          placeholder="请输入工人类型名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付宝" prop="zfbdz">
        <el-input
          v-model="queryParams.zfbdz"
          placeholder="请输入支付宝"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="微信支付地址" prop="wxzfdz">
        <el-input
          v-model="queryParams.wxzfdz"
          placeholder="请输入微信支付地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工人编号" prop="grbh">
        <el-input
          v-model="queryParams.grbh"
          placeholder="请输入工人编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工人注册时间" prop="grzcsj">
        <el-date-picker clearable
          v-model="queryParams.grzcsj"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择工人注册时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="从业年限" prop="cynx">
        <el-input
          v-model="queryParams.cynx"
          placeholder="请输入从业年限"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="健康证地址" prop="jkz">
        <el-input
          v-model="queryParams.jkz"
          placeholder="请输入健康证地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="cjsj">
        <el-date-picker clearable
          v-model="queryParams.cjsj"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="修改时间" prop="xgsj">
        <el-date-picker clearable
          v-model="queryParams.xgsj"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择修改时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="操作人" prop="operate">
        <el-input
          v-model="queryParams.operate"
          placeholder="请输入操作人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="登录人手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入登录人手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:employee:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:employee:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:employee:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:employee:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="employeeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="工人ID" align="center" prop="id" />
      <el-table-column label="工人编号" align="center" prop="employeeNo" />
      <el-table-column label="姓名" align="center" prop="xm" />
      <el-table-column label="员工年龄" align="center" prop="age" />
      <el-table-column label="家庭住址" align="center" prop="jtzz" />
      <el-table-column label="出生年月" align="center" prop="csny" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.csny, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="电话" align="center" prop="tel" />
      <el-table-column label="性别" align="center" prop="xb" />
      <el-table-column label="身份证号码" align="center" prop="sfzhm" />
      <el-table-column label="身份证正面" align="center" prop="sfzdzZm" />
      <el-table-column label="身份证反面" align="center" prop="sfzdzFm" />
      <el-table-column label="头像" align="center" prop="tx" />
      <el-table-column label="工人类型ID" align="center" prop="grlx" />
      <el-table-column label="工人类型名称" align="center" prop="grlxmc" />
      <el-table-column label="支付宝" align="center" prop="zfbdz" />
      <el-table-column label="微信支付地址" align="center" prop="wxzfdz" />
      <el-table-column label="工人编号" align="center" prop="grbh" />
      <el-table-column label="工人注册时间" align="center" prop="grzcsj" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.grzcsj, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="自我介绍" align="center" prop="zwjs" />
      <el-table-column label="从业年限" align="center" prop="cynx" />
      <el-table-column label="健康证地址" align="center" prop="jkz" />
      <el-table-column label="创建时间" align="center" prop="cjsj" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.cjsj, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="xgsj" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.xgsj, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作人" align="center" prop="operate" />
      <el-table-column label="登录人手机号" align="center" prop="phone" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:employee:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:employee:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改员工对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="工人编号" prop="employeeNo">
          <el-input v-model="form.employeeNo" placeholder="请输入工人编号" />
        </el-form-item>
        <el-form-item label="姓名" prop="xm">
          <el-input v-model="form.xm" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="员工年龄" prop="age">
          <el-input v-model="form.age" placeholder="请输入员工年龄" />
        </el-form-item>
        <el-form-item label="家庭住址" prop="jtzz">
          <el-input v-model="form.jtzz" placeholder="请输入家庭住址" />
        </el-form-item>
        <el-form-item label="出生年月" prop="csny">
          <el-date-picker clearable
            v-model="form.csny"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择出生年月">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="电话" prop="tel">
          <el-input v-model="form.tel" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="性别" prop="xb">
          <el-input v-model="form.xb" placeholder="请输入性别" />
        </el-form-item>
        <el-form-item label="身份证号码" prop="sfzhm">
          <el-input v-model="form.sfzhm" placeholder="请输入身份证号码" />
        </el-form-item>
        <el-form-item label="身份证正面" prop="sfzdzZm">
          <el-input v-model="form.sfzdzZm" placeholder="请输入身份证正面" />
        </el-form-item>
        <el-form-item label="身份证反面" prop="sfzdzFm">
          <el-input v-model="form.sfzdzFm" placeholder="请输入身份证反面" />
        </el-form-item>
        <el-form-item label="头像" prop="tx">
          <el-input v-model="form.tx" placeholder="请输入头像" />
        </el-form-item>
        <el-form-item label="工人类型ID" prop="grlx">
          <el-input v-model="form.grlx" placeholder="请输入工人类型ID" />
        </el-form-item>
        <el-form-item label="工人类型名称" prop="grlxmc">
          <el-input v-model="form.grlxmc" placeholder="请输入工人类型名称" />
        </el-form-item>
        <el-form-item label="支付宝" prop="zfbdz">
          <el-input v-model="form.zfbdz" placeholder="请输入支付宝" />
        </el-form-item>
        <el-form-item label="微信支付地址" prop="wxzfdz">
          <el-input v-model="form.wxzfdz" placeholder="请输入微信支付地址" />
        </el-form-item>
        <el-form-item label="工人编号" prop="grbh">
          <el-input v-model="form.grbh" placeholder="请输入工人编号" />
        </el-form-item>
        <el-form-item label="工人注册时间" prop="grzcsj">
          <el-date-picker clearable
            v-model="form.grzcsj"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择工人注册时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="自我介绍" prop="zwjs">
          <el-input v-model="form.zwjs" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="从业年限" prop="cynx">
          <el-input v-model="form.cynx" placeholder="请输入从业年限" />
        </el-form-item>
        <el-form-item label="健康证地址" prop="jkz">
          <el-input v-model="form.jkz" placeholder="请输入健康证地址" />
        </el-form-item>
        <el-form-item label="创建时间" prop="cjsj">
          <el-date-picker clearable
            v-model="form.cjsj"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="修改时间" prop="xgsj">
          <el-date-picker clearable
            v-model="form.xgsj"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择修改时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="操作人" prop="operate">
          <el-input v-model="form.operate" placeholder="请输入操作人" />
        </el-form-item>
        <el-form-item label="登录人手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入登录人手机号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listEmployee, getEmployee, delEmployee, addEmployee, updateEmployee } from "@/api/system/employee";

export default {
  name: "Employee",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 员工表格数据
      employeeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        employeeNo: null,
        xm: null,
        age: null,
        jtzz: null,
        csny: null,
        tel: null,
        xb: null,
        sfzhm: null,
        sfzdzZm: null,
        sfzdzFm: null,
        tx: null,
        grlx: null,
        grlxmc: null,
        zfbdz: null,
        wxzfdz: null,
        grbh: null,
        grzcsj: null,
        zwjs: null,
        cynx: null,
        jkz: null,
        cjsj: null,
        xgsj: null,
        operate: null,
        phone: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        xm: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询员工列表 */
    getList() {
      this.loading = true;
      listEmployee(this.queryParams).then(response => {
        this.employeeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        employeeNo: null,
        xm: null,
        age: null,
        jtzz: null,
        csny: null,
        tel: null,
        xb: null,
        sfzhm: null,
        sfzdzZm: null,
        sfzdzFm: null,
        tx: null,
        grlx: null,
        grlxmc: null,
        zfbdz: null,
        wxzfdz: null,
        grbh: null,
        grzcsj: null,
        zwjs: null,
        cynx: null,
        jkz: null,
        cjsj: null,
        xgsj: null,
        operate: null,
        phone: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加员工";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getEmployee(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改员工";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateEmployee(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEmployee(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除员工编号为"' + ids + '"的数据项？').then(function() {
        return delEmployee(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/employee/export', {
        ...this.queryParams
      }, `employee_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
